﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;


namespace ExameRegister.Web.Sevice
{
    public class ExcuteWCFService
    {
        public static object  ExcuteServiceAction(string classname, string methodname, System.Collections.ObjectModel.ObservableCollection<string> Parameters)
        {
            //Assembly asmb = Assembly.LoadFrom("EnterpriseServerBase.dll");
            string path = System.AppDomain.CurrentDomain.BaseDirectory;
            int index =  classname.LastIndexOf(".");
            string ns = classname.Substring(0, index);
            ns = path + "bin\\" + ns + ".dll";
            Assembly asmb = Assembly.LoadFrom(ns);
            var s = Activator.CreateInstance(asmb.GetType(classname));
            Assembly.LoadFrom(path + "bin\\" + "ExameRegister.Model" + ".dll");
            MethodInfo mi = s.GetType().GetMethod(methodname);
            ParameterInfo[] pis = mi.GetParameters();
            object[] _Params = null;
            if (Parameters != null && pis != null && Parameters.Count == pis.Count())
            {
                int count = Parameters.Count;
                _Params = new object[count];
                for (int i = 0; i < count; i++)
                {
                    Type t = pis[i].ParameterType;
                    var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                    //JsonFeaturedOffer deserializedObject = serializer.Deserialize<MenuItem>(s_JsonBaseDate);
                    _Params[i] = Utility.ConvertFromJson(Parameters[i], t);
                }
            }
            if (_Params != null)
            {
               return  mi.Invoke(s, _Params);
            }
            return null;
        }
    }
}